﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SinaWeiboApp_sl.Command;
using SinaWeiboApp_sl.View;
using System.Windows.Media.Imaging;
using SinaWeiboClientV1;
using ViewModels;
using System.Threading.Tasks;

namespace SinaWeiboApp_sl.ViewModel
{
    public class MainViewModel : ViewModelBase<MainViewModel>
    {
        public MainView View { get; set; }

        SinaWeiboClientV1.WeiboClient _sinaWeibo;



        protected override System.Threading.Tasks.Task BeginInitActions()
        {
            return Task.Factory.StartNew(() => { });
        }

        protected override System.Threading.Tasks.Task EndInitActions()
        {
            return Task.Factory.StartNew(() => { });
        }
            
        public UsersShowResponse UserShow
        {
            get { return _UserShowContainerLocator(this).Value; }
            set { _UserShowContainerLocator(this).SetValueAndTryNotify(value); }
        }        
        #region Property UsersShowResponse UserShow Setup        
        protected PropertyContainer<UsersShowResponse> _UserShow;  
        protected static Func<object, PropertyContainer<UsersShowResponse>> _UserShowContainerLocator =
            RegisterContainerLocator<UsersShowResponse>(
                "UserShow",
                model =>
                    model._UserShow = 
                        model._UserShow
                        ??
                        new PropertyContainer<UsersShowResponse>("UserShow"));
        #endregion
        
        
        
        




        public BitmapImage ProfileImage
        {
            get { return _ProfileImageContainerLocator(this).Value; }
            set { _ProfileImageContainerLocator(this).SetValueAndTryNotify(value); }
        }
        #region Property BitmapImage ProfileImage Setup
        protected PropertyContainer<BitmapImage> _ProfileImage;
        protected static Func<object, PropertyContainer<BitmapImage>> _ProfileImageContainerLocator =
            RegisterContainerLocator<BitmapImage>(
                "ProfileImage",
                model =>
                    model._ProfileImage =
                        model._ProfileImage
                        ??
                        new PropertyContainer<BitmapImage>("ProfileImage"));
        #endregion




        

        
        public MainViewModel(WeiboClient sinaWeibo = null)
        {
            if (sinaWeibo == null)
            {
                sinaWeibo = new WeiboClient();
            }
            _sinaWeibo = sinaWeibo;


            ShowUser();
        }

        public async Task ShowUser()
        {
            var request = new SinaWeiboClientV1.UsersShowRequest { Id = _sinaWeibo.CurrentAuthToken.ContextData["UserID"] as string  };
            var response = await _sinaWeibo.ExecuteUsersShowAsync(request);
            UserShow = response.Value;
            ProfileImage = new BitmapImage(new Uri(UserShow.ProfileImageUrl));
        }
    }
}
